//
//  KLSGMineViewController.m
//  KaoLaShiGuangFM
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "KLSGMineViewController.h"
#import "KLSGShengMingController.h"
#import "AppDelegate.h"
#import "KLSGCollectionViewController.h"
#import "HUDHeaper.h"
#import "KLSGRadioPlayManager.h"

@interface KLSGMineViewController ()<UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate>
@property (nonatomic, strong) NSArray *cachespath;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *lable;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation KLSGMineViewController
{
    int i;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addSubView];
}

- (void)addSubView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewScrollPositionNone;
    self.tableView.bounces = NO;
    [self.view addSubview:_tableView];
}

#pragma mark -实现tableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] init];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"收藏";
            break;
        case 1:
        {
            self.lable = [[UILabel alloc] initWithFrame:CGRectMake(self.tableView.frame.size.width, 7, 50, 30)];
//            self.lable.backgroundColor = [UIColor redColor];
            self.lable.font = [UIFont systemFontOfSize:13];
            self.lable.hidden = YES;
            [cell addSubview:self.lable];
            
            cell.textLabel.text = @"定时关闭";
            break;
        }
        case 2:
            cell.textLabel.text = @"清除缓存";
            break;
        case 3:
        {
            cell.textLabel.text = @"夜间模式";
            UISwitch *mySitch = [[UISwitch alloc] initWithFrame:CGRectMake(self.tableView.frame.size.width, 7, 50, 30)];
            mySitch.onTintColor = [UIColor colorWithRed:234 / 256.0 green:79 / 256.0 blue:56 / 256.0 alpha:1];
            [mySitch addTarget:self action:@selector(actionSwitch:) forControlEvents:(UIControlEventValueChanged)];
            [cell addSubview:mySitch];
            cell.accessoryType = UITableViewCellAccessoryNone;
            break;
        }
        case 4:
            cell.textLabel.text = @"免责声明";
            break;
        case 5:
            cell.textLabel.text = @"关于";
            break;
            
        default:
            break;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kScreenHeight / 4;
    
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIImageView *headView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
//    headView.image = [UIImage imageNamed:@"leftBg2"];
//    headView.backgroundColor = [UIColor whiteColor];
//    return headView;
//}
#pragma mark - 跳转登陆页面
//- (void)actionLogin
//{
//    
//}

#pragma mark - 跳转详情页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {  // 收藏
        KLSGCollectionViewController *collectionVC = [[KLSGCollectionViewController alloc] init];
        AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        [appDelegate.leftSliderVC.navigationController pushViewController:collectionVC animated:YES];

    } else if (indexPath.row == 1) {  // 定时关闭
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"定时关闭" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"15分钟", @"30分钟", @"60分钟", @"取消定时", nil];
        [actionSheet showInView:self.view];
    } else if (indexPath.row == 2) {  //  清除缓存
        NSArray *cachesPath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        self.cachespath=[cachesPath lastObject];
        //NSLog(@"%@",self.cachespath);
        float size=[self folderSizeAtPath:(NSString *)self.cachespath];
        NSString *message=[NSString stringWithFormat:@"缓存的大小为%.2fM,确定要清理缓存吗?",size];
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alertView show];
        
    } else if (indexPath.row == 4) {  //  免责声明
        KLSGShengMingController *shengMingVC = [[KLSGShengMingController alloc] init];
        AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        [appDelegate.leftSliderVC presentViewController:shengMingVC animated:YES completion:nil];
    } else if (indexPath.row == 5) {  //  关于
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"设计人" message:@"" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"考拉时光工作室" style:(UIAlertActionStyleDefault) handler:nil];
        [alertC addAction:alertAction];
        [self presentViewController:alertC animated:YES completion:nil];
    }
}

#pragma mark -计算缓存
- (float)folderSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
//            folderSize +=[NSFileManager fileSizeAtPath:absolutePath];
        }
        //SDWebImage框架自身计算缓存的实现
        folderSize+=[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
        //NSLog(@"%f",folderSize);
        return folderSize;
    }
    return 0;
}
#pragma mark -清理缓存
- (void)clearCache:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] cleanDisk];
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {
        return;
    }else{
        [self clearCache:(NSString *)self.cachespath];
        [HUDHeaper addHUDInView:self.view text:@"清理成功" hideAfterDelay:1];
    }
}


- (void)actionSwitch:(UISwitch *)mySwitch
{
    if (mySwitch.on) {
        [[UIApplication sharedApplication].delegate window].alpha = 0.3;
    } else {
        [[UIApplication sharedApplication].delegate window].alpha = 1;
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(actionTimer:) userInfo:nil repeats:YES];
    [self.timer fire];
    
    switch (buttonIndex) {
        case 0:
        {
            i = 15 * 60;
            self.lable.hidden = NO;
            break;
        }
        case 1:
        {
            i = 30 * 60;
            self.lable.hidden = NO;
            break;
        }
        case 2:
        {
            i = 60 * 60;
            self.lable.hidden = NO;
            break;
        }
        case 3:
        {
            self.lable.hidden = YES;
        }
        default:
            break;
    }
}
- (void)actionTimer:(NSTimer *)timer
{
    i--;
    self.lable.text = [NSString stringWithFormat:@"%d:%.2d", i / 60, i % 60];
    if (i == 0) {
        [[KLSGRadioPlayManager shareManager] musicPause];
        self.lable.hidden = YES;
        [timer invalidate];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
